package my.com.epetrol.rapidkl.controller;

import my.com.epetrol.rapidkl.R;
import my.com.epetrol.rapidkl.model.MapPoint;
import my.com.epetrol.rapidkl.model.Point;

import com.u1aryz.android.lib.newpopupmenu.MenuItem;
import com.u1aryz.android.lib.newpopupmenu.PopupMenu;
import com.u1aryz.android.lib.newpopupmenu.PopupMenu.OnItemSelectedListener;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import uk.co.senab.photoview.PhotoViewAttacher;
import uk.co.senab.photoview.PhotoViewAttacher.OnPhotoTapListener;

public class MapHandler implements OnPhotoTapListener, OnItemSelectedListener {
	private static final Point[] points = new Point[] {
		MapPoint.createPoint(268f, 18f, 299f, 32f, R.drawable.ag8, "Ampang"),
		MapPoint.createPoint(268f, 48f, 299f, 62f, R.drawable.ag7, "Cahaya"),
		MapPoint.createPoint(268f, 78f, 299f, 92f, R.drawable.ag6, "Cempaka"),
		MapPoint.createPoint(268f, 108f, 299f, 122f, R.drawable.ag5, "Pandan Indah"),
		MapPoint.createPoint(268f, 138f, 299f, 152f, R.drawable.ag4, "Pandan Jaya"),
		MapPoint.createPoint(268f, 168f, 299f, 182f, R.drawable.ag3, "Maluri"),
		MapPoint.createPoint(268f, 198f, 299f, 212f, R.drawable.ag2, "Miharja"),
		
		MapPoint.createPoint(344f, 18f, 375f, 32f, R.drawable.ph8, "Sri Petaling"),
		MapPoint.createPoint(344f, 48f, 375f, 62f, R.drawable.ph7, "Bukit Jalil"),
		MapPoint.createPoint(344f, 78f, 375f, 92f, R.drawable.ph6, "Sungai Besi"),
		MapPoint.createPoint(344f, 108f, 375f, 122f, R.drawable.ph5, "Bandar Tasik Selatan"),
		MapPoint.createPoint(344f, 138f, 375f, 152f, R.drawable.ph4, "Bandar Tun Razak"),
		MapPoint.createPoint(344f, 168f, 375f, 182f, R.drawable.ph3, "Salak Selatan"),
		MapPoint.createPoint(344f, 198f, 375f, 212f, R.drawable.ph2, "Cheras"),
		
		MapPoint.createPoint(306f, 302f, 337f, 316f, R.drawable.st2, "Pudu"),
		MapPoint.createPoint(271f, 332f, 337f, 346f, R.drawable.mr4_st3, "Hang Tuah"),
		MapPoint.createPoint(306f, 362f, 337f, 376f, R.drawable.st4, "Plaza Rakyat"),
		MapPoint.createPoint(271f, 392f, 337f, 406f, R.drawable.kj13_st5, "Masjid Jamek"),
		MapPoint.createPoint(306f, 422f, 337f, 436f, R.drawable.st6, "Bandaraya"),
		MapPoint.createPoint(306f, 452f, 337f, 466f, R.drawable.st7, "Sultan Ismail"),
		MapPoint.createPoint(306f, 302f, 337f, 316f, R.drawable.st8, "PWTC"),
		
		MapPoint.createPoint(236f, 272f, 337f, 286f, R.drawable.ph1_ag1_st1, "Chan Sow Lin")
	};
	private static final float IMAGE_WIDTH = 575f;
	private static final float IMAGE_HEIGHT = 605f;
	
	private static final int POP_MENU_FROM = 1;
	private static final int POP_MENU_TO = 2;
	private static final int POP_MENU_FREQUENCY = 3;
	private static final int POP_MENU_OPERATING_HOURS = 4;
	
	private Drawable mapDrawable;
	private SelectedPoint circleFrom;
	private SelectedPoint circleTo;
	private Point selected;
	
	private ImageView imageView;
	private Activity activity;
	private PhotoViewAttacher viewAttacher;
	private EditText fromTxt;
	private EditText toTxt;
	private PopupMenu menu;
	private View anchor;
	private TextView logTxt;
	
	public MapHandler(Activity activity) {
		this.activity = activity;
		
		mapDrawable = getDrawable(R.drawable.ampangmap);
		circleFrom = new SelectedPoint(getDrawable(R.drawable.empty), false);
		circleTo = new SelectedPoint(getDrawable(R.drawable.empty), false);
		
		imageView = (ImageView) activity.findViewById(R.id.mapView);
		imageView.setImageDrawable(new LayerDrawable( new Drawable[] {mapDrawable, circleFrom.drawable, circleTo.drawable}));
		viewAttacher = new PhotoViewAttacher(imageView);
		viewAttacher.setOnPhotoTapListener(this);
		
		fromTxt = (EditText) activity.findViewById(R.id.fromTxt);
		toTxt = (EditText) activity.findViewById(R.id.toTxt);
		logTxt = (TextView) activity.findViewById(R.id.log);
		
		menu = new PopupMenu(activity) {
			@Override
			public void onPopupDismiss() {
				log("resetting image layers.");
				resetImageLayers();
			}
		};
		menu.add(POP_MENU_FROM, R.string.fromLabel);
		menu.add(POP_MENU_TO, R.string.toTxt);
		menu.add(POP_MENU_FREQUENCY, R.string.frequency);
		menu.add(POP_MENU_OPERATING_HOURS, R.string.operatingHours);
		menu.setOnItemSelectedListener(this);
		
	}
	
	public Activity getActivity() {
		return activity;
	}
	
	private void log(String msg) {
		logTxt.setText(msg);
	}
	
	private Drawable getDrawable(int id) {
		return activity.getResources().getDrawable(id);
	}

	@Override
	public void onPhotoTap(View view, float x, float y) {
		Point selectedPoint = null;
		anchor = view;
		
		for (Point point : points) {
			if (point.inBound(x, y, IMAGE_WIDTH, IMAGE_HEIGHT)) {
				log("inbound for " + point.getLabel());
				selectedPoint = point;
				break;
			}
		}
		
		if (selectedPoint != null) {
			drawCircleOn(selectedPoint);
			showPopup(selectedPoint);
		}
	}
	
	private void showPopup(Point selectedPoint) {
		this.selected = selectedPoint;
		menu.show(anchor);
	}
	
	@Override
	public void onItemSelected(MenuItem item) {
		onPopupMenuSelected(item.getItemId(), selected);
	}

	/**
	 * This will be fired when the pop up menu is selected.
	 * 
	 * @param item
	 * @param selectedPoint
	 */
	public void onPopupMenuSelected(int item, Point selectedPoint) {
		switch (item) {
			case POP_MENU_FROM:
				circleFrom.drawable = getDrawable(selectedPoint.getDrawableId());
				circleFrom.filled = true;
				fromTxt.setText(selectedPoint.getLabel());
				break;
			
			case POP_MENU_TO:
				circleTo.drawable = getDrawable(selectedPoint.getDrawableId());
				circleTo.filled = true;
				toTxt.setText(selectedPoint.getLabel());
				break;
			
			case POP_MENU_FREQUENCY:
				break;
			
			case POP_MENU_OPERATING_HOURS:
				break;
		}
	}

	/**
	 * Call this to reset the image layers.
	 * It will remove any unnessary circles.
	 * 
	 */
	private void resetImageLayers() {
		if (!circleFrom.filled) {
			circleFrom.drawable = getDrawable(R.drawable.empty);
			fromTxt.setText("");
		}
		
		if (!circleTo.filled) {
			circleTo.drawable = getDrawable(R.drawable.empty);
			toTxt.setText("");
		}
		imageView.setImageDrawable(new LayerDrawable(new Drawable[] {mapDrawable, circleFrom.drawable, circleTo.drawable}));
	}
	
	/**
	 * Draw the circles including currently selected.
	 * 
	 * @param point
	 */
	private void drawCircleOn(Point point) {
		Drawable currentlySelected = getDrawable(point.getDrawableId());
		imageView.setImageDrawable(new LayerDrawable(new Drawable[] {mapDrawable, circleFrom.drawable, circleTo.drawable, currentlySelected}));
	}
	
	private static class SelectedPoint {
		private Drawable drawable;
		private boolean filled = false;
		
		public SelectedPoint(Drawable drawable, boolean filled) {
			this.drawable = drawable;
			this.filled = filled;
		}
	}
}
